package greenteam.mylook;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import registration.RegisterActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class LoginActivity extends Activity 
{
	int login_ok = 0;  // -1 no connessione, 0 dati errati, 1 tutto ok
	ProgressDialog spinner;
    Thread loginThread;
	Handler loginHandler;
    ImageButton btnLogin, btnRegister;
    EditText txtUsername, txtPassword;
    String username, password;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

        btnRegister = (ImageButton)findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                finish();
            }

        });

        spinner = new ProgressDialog(this);
        spinner.setTitle(R.string.loginDialogTitle);
        spinner.setMessage(getResources().getString(R.string.loginDialogText));
        spinner.setCancelable(false);
        spinner.setIndeterminate(false);
        spinner.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        loginThread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                spinner.show();
                Looper.prepare();

                if(PhoneUtilities.ConnectionEnabled(LoginActivity.this))
                {
                    try
                    {
                        login_ok = WebService.CheckLogin(username, password) ? 1 : 0;
                    }
                    catch (ClientProtocolException e)
                    {
                        e.printStackTrace();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }

                    loginHandler.sendEmptyMessage(0);
                }
                else
                    login_ok = -1;

                Looper.loop();
            }
        });

        loginHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                spinner.dismiss();

                if(login_ok == 1)
                {
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                }
                else if(login_ok == 0)
                    Toast.makeText(LoginActivity.this, R.string.loginDataError, Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(LoginActivity.this, R.string.loginInternetError, Toast.LENGTH_LONG).show();
            }
        };


        txtUsername = (EditText)findViewById(R.id.txt_login_email);
        txtPassword = (EditText)findViewById(R.id.txt_login_password);

		btnLogin = (ImageButton)findViewById(R.id.btn_login);
		btnLogin.setOnClickListener(new View.OnClickListener()
        {
			@Override
			public void onClick(View v) 
			{
				username = txtUsername.getText().toString();
				password = txtPassword.getText().toString();
				loginThread.start();
			}
		});
	}
}
